package com.whh.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.whh.config.exception.APIException;
import com.whh.config.vo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whh.dao.LoginDao;
import com.whh.dao.MassageDao;
import com.whh.entity.ResultEntity;
import com.whh.entity.SysMassage;
import com.whh.entity.SysUserEntity;
import com.whh.service.LoginService;
import com.whh.utils.StrUtils;

@Service
public class LoginServiceImpl extends BaseServiceImpl<SysUserEntity> implements LoginService {

	@Autowired
	LoginDao dao;
	
	@Autowired
	MassageDao massage;

	//设置日期格式
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	

	@Override
	public SysUserEntity commit(SysUserEntity entity) {
		// TODO Auto-generated method stub
		String password = entity.getPassword();
		password = StrUtils.md5Encrypt(password);
		entity.setPassword(password);
		entity = dao.query(entity);
		if(entity != null) {
			entity.setVisitsNum(entity.getVisitsNum()+1);
			entity.setLastLoginTime(df.format(new Date()));
			dao.update(entity);
			SysMassage msg = new SysMassage();
			msg.setAction("登录了");
			msg.setOperator(entity.getName());
			msg.setOperatorId(entity.getId());
			msg.setObject("系统");
			msg.setObjectId(0);
			msg.setObjectName(entity.getLoginName());
			massage.add(msg);
		}else {
			throw new APIException(ResultCode.ERROR,"用户名或密码错误");
		}
		return entity;
	}

}
